﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MMOShared
{
    [Serializable]
    public class ServerEntry {
        public string HostAddress { get; private set; }
        public bool IsAvailable { get; private set; }
        public string ServerName { get; private set; }
        public int ServerPort { get; private set; }
        public ServerEntry(string servername,string hostaddress,bool avail,int port){
            HostAddress = hostaddress;
            ServerName = servername;
            IsAvailable = avail;
            ServerPort = port;
        }
    }
    [Serializable]
    public class ServerList
    {
        public List<ServerEntry> Servers { get; private set; }
        public int Count { get; private set; }
        public ServerList(ServerEntry[] servs)
        {
            Servers = new List<ServerEntry>();
            if (servs.Length > 0)
            {
                Count = servs.Length;
                foreach (ServerEntry serv in servs)
                {
                    Servers.Add(serv);
                }
            }
        }
        public ServerList()
        {
            Servers = new List<ServerEntry>();
        }
    }
    [Serializable]
    public class LoginDetails
    {
        public string Username { get; private set; }
        public string Password { get; private set; }
        public LoginDetails(string user, string pass)
        {
            if (user.Length > 12 || pass.Length > 12)
            {
                throw new Exception("Username or password longer than 12 characters!");
            }
            Username = user.ToLower();
            Password = GetMD5Hash(pass);
        }
        private string GetMD5Hash(string input)
        {
            System.Security.Cryptography.MD5CryptoServiceProvider x = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] bs = System.Text.Encoding.UTF8.GetBytes(input);
            bs = x.ComputeHash(bs);
            System.Text.StringBuilder s = new System.Text.StringBuilder();
            foreach (byte b in bs)
            {
                s.Append(b.ToString("x2").ToLower());
            }
            return s.ToString();
        }
    }
}
